#include "LoginProgressBtn.h"

#include <core/StyleMgr.h>

#include <QCursor>
#include <QMouseEvent>
#include <QStylePainter>

LoginProgressBtn::LoginProgressBtn(QWidget* parent /*= nullptr*/)
    : QWidget(parent) {}

void LoginProgressBtn::paintEvent(QPaintEvent* event) {
  QStylePainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);
  if (m_logining) {
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(StyleMgr::instance().color("StaticTextBK")));
    painter.drawRoundedRect(rect(), 2, 2);
    QRect progressR = rect();
    int width = (m_progress * progressR.width() / 100);
    if (width > 0) {
      progressR.setRight(progressR.left() + width - 1);
      painter.setBrush(QBrush(StyleMgr::instance().color("ButtonNormal")));
      painter.drawRoundedRect(progressR, 2, 2);
      progressR.setLeft(progressR.right() - 2);
      painter.drawRect(progressR);
    }

    QRect promptR = rect();
    QFont f = font();
    f.setPixelSize(15);
    painter.setFont(f);
    promptR.adjust(8, 0, 0, 0);
    painter.setPen(StyleMgr::instance().color("TextNormal"));
    painter.drawText(promptR, tr("正在登录..."),
                     QTextOption(Qt::AlignVCenter | Qt::AlignLeft));

    QRect percentR = rect();
    f.setPixelSize(13);
    painter.setFont(f);
    percentR.adjust(0, 0, -8, 0);
    QString str = QString("%1%").arg(m_progress);
    painter.drawText(percentR, str,
                     QTextOption(Qt::AlignVCenter | Qt::AlignRight));
  } else {
    if (m_state == State::Pressed) {
      QColor clr = StyleMgr::instance().color("ButtonPressed");
      painter.setBrush(QBrush(clr));
      painter.setPen(clr);
    } else {
      if (m_state == State::Hover) {
        QColor clr = StyleMgr::instance().color("Hover");
        painter.setBrush(QBrush(clr));
        painter.setPen(clr);
      } else {
        QColor clr = StyleMgr::instance().color("Button_L2");
        painter.setBrush(QBrush(clr));
        painter.setPen(clr);
      }
    }

    painter.drawRoundedRect(rect(), 2, 2);
    QPen pen(StyleMgr::instance().color("TextNormal"));
    painter.setPen(pen);
    QFont font = painter.font();
    font.setPixelSize(16);
    painter.setFont(font);
    painter.drawText(rect(), tr("登录"), QTextOption(Qt::AlignCenter));
  }
}

void LoginProgressBtn::mousePressEvent(QMouseEvent* event) {
  QWidget::mousePressEvent(event);
  if (event->button() == Qt::LeftButton && !m_logining) {
    m_state = State::Pressed;
    update();
    clicked();
  }
}

void LoginProgressBtn::mouseReleaseEvent(QMouseEvent* event) {
  QWidget::mouseReleaseEvent(event);
  if (event->button() == Qt::LeftButton && !m_logining) {
    m_state = State::Normal;
    update();
  }
}

void LoginProgressBtn::enterEvent(QEvent* event) {
  m_state = State::Hover;
  update();
}

void LoginProgressBtn::leaveEvent(QEvent* event) {
  m_state = State::Normal;
  update();
}

void LoginProgressBtn::setLoginProgress(int v) {
  m_progress = v;
  update();
}

void LoginProgressBtn::setLoginState(bool logining) {
  m_logining = logining;
  m_progress = 0;
  update();
}